﻿namespace Sidvall.CodeGenerator
{
	public static class ObjectExtension
	{
		#region IsDateTime

		public static bool IsDateTime(this object value)
		{
			System.Type type;
			System.DateTime? dateValue;

			if (value == null)
				return false;

			type = value as System.Type;
			if (type != null)
			{
				return type.IsDateTime();
			}
			else
			{
				if (value.GetType().IsDateTime())
					return true;
				if (value is string)
				{
					dateValue = value.ToNullableDateTime();
					if (dateValue != null)
						return true;
				}
			}

			return false;
		}

		#endregion
		#region IsNumeric

		public static bool IsNumeric(this object value)
		{
			System.Type type;
			decimal? numericValue;

			if (value == null)
				return false;

			type = value as System.Type;
			if (type != null)
			{
				return type.IsNumeric();
			}
			else
			{
				if (value.GetType().IsNumeric())
					return true;
				if (value is string)
				{
					numericValue = value.ToNullableDecimal();
					if (numericValue != null)
						return true;
				}
			}

			return false;
		}

		#endregion

		#region ToInt32

		public static int ToInt32(this object value, int defaultValue)
		{
			int? result;

			result = value.ToNullableInt32();
			if (result == null)
				return defaultValue;

			return result.Value;
		}

		#endregion

		#region ToNullableBoolean

		public static bool? ToNullableBoolean(this object value)
		{
			if (value == null)
				return null;
			if (value is bool)
				return (bool)value;
			return value.ToString(null).ToNullableBoolean();
		}

		#endregion
		#region ToNullableDateTime

		public static System.DateTime? ToNullableDateTime(this object value)
		{
			if (value == null)
				return null;
			if (value is System.DateTime)
				return (System.DateTime)value;
			return value.ToString(null).ToNullableDateTime();
		}

		#endregion
		#region ToNullableDecimal

		public static decimal? ToNullableDecimal(this object value)
		{
			if (value == null)
				return null;
			if (value is decimal)
				return (System.Decimal)value;
			return value.ToString(null).ToNullableDecimal();
		}

		#endregion
		#region ToNullableDouble

		public static double? ToNullableDouble(this object value)
		{
			if (value == null)
				return null;
			if (value is double)
				return (System.Double)value;
			return value.ToString(null).ToNullableDouble();
		}

		#endregion
		#region ToNullableByte

		public static byte? ToNullableByte(this object value)
		{
			if (value == null)
				return null;
			if (value is byte)
				return (byte)value;
			return value.ToString(null).ToNullableByte();
		}

		#endregion
		#region ToNullableInt16

		public static short? ToNullableInt16(this object value)
		{
			if (value == null)
				return null;
			if (value is short)
				return (short)value;
			return value.ToString(null).ToNullableInt16();
		}

		#endregion
		#region ToNullableInt32

		public static int? ToNullableInt32(this object value)
		{
			if (value == null)
				return null;
			if ((value is int) || (value is long))
				return (int)value;
			return value.ToString(null).ToNullableInt32();
		}

		#endregion
		#region ToNullableInt64

		public static long? ToNullableInt64(this object value)
		{
			if (value == null)
				return null;
			if (value is long)
				return (long)value;
			return value.ToString(null).ToNullableInt64();
		}

		#endregion
		#region ToNullableSingle

		public static float? ToNullableSingle(this object value)
		{
			if (value == null)
				return null;
			if (value is float)
				return (float)value;
			return value.ToString(null).ToNullableSingle();
		}

		#endregion
		#region ToNullableTimeSpan

		public static System.TimeSpan? ToNullableTimeSpan(this object value)
		{
			if (value == null)
				return null;
			if (value is System.TimeSpan)
				return (System.TimeSpan)value;
			return value.ToString(null).ToNullableTimeSpan();
		}

		#endregion
		#region ToString

		public static string ToString(this object value, string defaultValue)
		{
			System.Array arrayValue;
			System.Text.StringBuilder sb;
			string stringValue;
			int index;

			if (value == null)
				return defaultValue;

			stringValue = value as string;
			if (stringValue != null)
				return stringValue;

			arrayValue = value as System.Array;
			if (arrayValue != null)
			{
				index = 0;
				sb = new System.Text.StringBuilder();
				foreach (var item in arrayValue)
				{
					if (index > 0)
						sb.Append(",");
					sb.Append(item.ToString(defaultValue));
					index += 1;
				}
				return sb.ToString();
			}

			return value.ToString();
		}

		#endregion
	}
}
